package com.blog.framework.config.properties;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Pattern;
import org.apache.commons.lang3.RegExUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import com.blog.common.annotation.Anonymous;

/**
 * 设置Anonymous注解允许匿名访问的url
 * 
 * @author shibalover
 */
@Configuration
public class PermitAllUrlProperties implements InitializingBean, ApplicationContextAware
{
    //定义规则
    private static final Pattern PATTERN = Pattern.compile("\\{(.*?)\\}");
    //设置上下文对象
    private ApplicationContext applicationContext;
    //设置Anonymous注解的url都加入到这里
    private List<String> urls = new ArrayList<>();

    public String ASTERISK = "*";

    //项目初始化运行
    @Override
    public void afterPropertiesSet()
    {
        //获取所有bean对象
        RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
        //获取每一个方法
        //获取url与类和方法的对应关系
        // "{POST[/system/post]}": "package com.blog.web.controller.system.SysConfigController#list(SysConfig)
        Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();
        //info代表每一个url对象
        map.keySet().forEach(info -> {
            //获取类和方法的信息
            HandlerMethod handlerMethod = map.get(info);

            // 获取方法上边的注解 替代path variable 为 *
            //判断方法是否有Anonymous注解
            Anonymous method = AnnotationUtils.findAnnotation(handlerMethod.getMethod(), Anonymous.class);
            //判断是否为空，不为空执行后面
            Optional.ofNullable(method).ifPresent(
                    anonymous -> Objects.requireNonNull(
                            info.getPatternsCondition().getPatterns())
                    //替换
                    .forEach(
                            url -> urls.add(RegExUtils.replaceAll(url, PATTERN, ASTERISK))));

            // 获取类上边的注解, 替代path variable 为 *
            Anonymous controller = AnnotationUtils.findAnnotation(handlerMethod.getBeanType(), Anonymous.class);
            Optional.ofNullable(controller).ifPresent(
                    anonymous -> Objects.requireNonNull(info.getPatternsCondition().getPatterns())
                    .forEach(
                            url -> urls.add(RegExUtils.replaceAll(url, PATTERN, ASTERISK))));
        });
    }

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException
    {
        this.applicationContext = context;
    }

    public List<String> getUrls()
    {
        return urls;
    }

    public void setUrls(List<String> urls)
    {
        this.urls = urls;
    }
}
